'''
Author: LHY 2601958675@qq.com
Date: 2023-06-29 15:42:47
LastEditors: LHY
LastEditTime: 2023-06-29 17:43:17
Description: 爬豆瓣昵称
'''
import re
import requests

f = open('wa.csv',mode='w',encoding='utf-8')

url = "https://movie.douban.com/"

headers = {"Referer":"https://movie.douban.com/","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
respon = requests.get(url,headers=headers)
responTetx = respon.text
# print(respon.text)

s = """
<td class="title"><a onclick="moreurl(this, {from:'mv_rk'})" href="https://movie.douban.com/subject/35056376/">惊天营救2</a></td>
"""

h = """
 <li class="poster">
"""
 
obj = re.compile(r'<li class="poster">(.*?)<a onclick="moreurl\(this, {from:\'mv_a_tl\'}\)" href="(.*?)" class="">(?P<name>.*?)</a>(.*?)<span class="rating-star (.*?)"></span><span class="subject-rate">(?P<score>.*?)</span>(.*?)<li class="ticket_btn"><span><a onclick="moreurl\(this, {from:\'mv_b_tc\'}\)" href="(.*?)" target="_blank">(?P<detail>.*?)</a></span></li>',re.S)#re.S可以让.匹配换行

re = obj.finditer(responTetx)
for item in re:
    name = item.group("name")
    score = item.group("score")
    detail = item.group("detail")
    print(name)
    print(score)
    print(detail)
    f.write(f'{name},{score},{detail}\n')


f.close()
respon.close()
print('done')